package by.alex.dzeshko.lastfmsimple.bo;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import by.alex.dzeshko.lastfmsimple.bo.common.JSONModel;

public class ArtistModel extends JSONModel {

	private static final String ALBUM = "album";
	private static final String TOPALBUMS = "topalbums";
	private static final String ARTIST2 = "artist";
	private static final String SIMILAR = "similar";
	private static final String BIO = "bio";
	private static final String SUMMARY = "summary";
	private static final String ARTIST = ARTIST2;
	private static final String ARTISTMATCHES = "artistmatches";
	private static final String LOG_TAG = "ArtistModel";

	public ArtistModel() {
		super();
	}

	public ArtistModel(JSONObject jsonObject) {
		super(jsonObject);
	}

	public ArtistModel(String jsonString) throws JSONException {
		super(jsonString);
	}

	@Override
	public String getName() throws JSONException {
		return super.getName();
	}

	@Override
	public String getMbid() throws JSONException {
		return super.getMbid();
	}

	@Override
	public String getImage(int size) throws JSONException {
		return super.getImage(size);
	}

	public String getSummary() throws JSONException {
		return getStringFromObject(BIO, SUMMARY);
	}
	
	public static List<JSONObject> getArtistList(Object source) throws JSONException {
		return getObjectsList(source, ARTISTMATCHES, ARTIST);
	}
	
	public JSONArray getSimilarArray() throws JSONException{
		return jo.getJSONObject(SIMILAR).getJSONArray(ARTIST2);
	}
	
	public static List<JSONObject> getAlbums(JSONObject source) throws JSONException {
		if(source == null) {
			throw new IllegalArgumentException("String source is null");
		}
		JSONArray array = source.getJSONObject(TOPALBUMS).getJSONArray(ALBUM);
		if(array.length()==0) {
			Log.d(LOG_TAG, "array size 0");
			return new ArrayList<JSONObject>();
		}
		List<JSONObject> list = new ArrayList<JSONObject>(array.length());
		for (int i = 0; i < array.length(); i++) {
			JSONObject album = array.getJSONObject(i);
			JSONObject artist = album.getJSONObject(ARTIST);
			String artistname = artist.getString(NAME);
			album.put(ARTIST, artistname);
			list.add(album);
		}
		return list;
	}
}
